package servlet;

import bean.Student;
import service.BanjiService;
import service.BanjiServiceImpl;
import service.StudentService;
import service.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "BanjiFindServlet" ,urlPatterns = "/banjiFindServlet")
public class BanjiFindServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //解决中文乱码问题
        request.setCharacterEncoding ("utf-8");
        response.setContentType ("text/html;charset=utf-8");
        //1.接收前端的班级信息
        String bName = request.getParameter ("bName");
        System.out.println (bName);
        //2.通过servlet查询班级信息
        BanjiService banjiService = new BanjiServiceImpl ();
        List<Student> studentList = banjiService.BanjiFindService (bName);
        request.getSession ().setAttribute ("studentlist",studentList);
        StudentService studentService = new StudentServiceImpl ();
        //3.向前端页面做出响应
        PrintWriter writer = response.getWriter ();
        //获取模块信息
        String contextPath = this.getServletContext ().getContextPath ();
        //请求拼接路径
        String hrefPath = contextPath + "/banjiQueryServlet";
        if (studentList.size () > 0) {
            // request.getSession().setAttribute("studentList", studentList);
            writer.print("<script>alert('查找班级成功！'); location.href='" + hrefPath + "';</script>");
        } else {
            writer.print("<script>alert('查找班级失败！'); location.href='" + hrefPath + "';</script>");
        }
        System.out.println (studentList.size ());
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost (request,response);
    }
}
